﻿using Bottle.CommunityForum.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Bottle.CommunityForum.DAL
{
    public class BaseDal<T> where T : class, new()
    {
        public DbContext Db
        {
            get { return DbContextFactory.GetCurrentDbContext(); }
        }

        #region 查询

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="wherelambda"></param>
        /// <returns></returns>
        public IQueryable<T> QueryEntitys(Expression<Func<T, bool>> wherelambda)
        {
            return Db.Set<T>().Where(wherelambda)
                .AsQueryable();
        }

        /// <summary>
        /// 分页查询(order by)
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="totle"></param>
        /// <param name="wherelambda"></param>
        /// <param name="ordlambda"></param>
        /// <returns></returns>
        public IQueryable<T> QueryPageOrdEntitys<S>(int pageSize,int pageIndex,out int totle,Expression<Func<T,bool>> wherelambda,Expression<Func<T,S>> ordlambda)
        {
            totle = Db.Set<T>().Where(wherelambda).Count();
            return Db.Set<T>().Where(wherelambda)
                .OrderBy(ordlambda)
                .Skip(pageSize * (pageIndex - 1))
                .Take(pageSize)
                .AsQueryable();
        }

        /// <summary>
        /// 分页查询(order by desc)
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="totle"></param>
        /// <param name="wherelambda"></param>
        /// <param name="ordlambda"></param>
        /// <returns></returns>
        public IQueryable<T> QueryPageDescEntitys<S>(int pageSize, int pageIndex, out int totle, Expression<Func<T, bool>> wherelambda, Expression<Func<T, S>> ordlambda)
        {
            totle = Db.Set<T>().Where(wherelambda).Count();
            return Db.Set<T>().Where(wherelambda)
                .OrderByDescending(ordlambda)
                .Skip(pageSize * (pageIndex - 1))
                .Take(pageSize)
                .AsQueryable();
        }
        #endregion

        #region 添加

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T AddEntity(T entity)
        {
            Db.Set<T>().Add(entity);
            //Db.SaveChanges();
            return entity;
        }
        #endregion

        #region 修改

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool EditEntity(T entity)
        {
            Db.Entry(entity).State = EntityState.Modified;
            //return Db.SaveChanges() > 0;
            return true;
        }
        #endregion

        #region 删除

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DelEntity(T entity)
        {
            Db.Entry(entity).State = EntityState.Deleted;
            //return Db.SaveChanges() > 0;
            return true;
        }

        #endregion
    }
}
